<?php
    class SF_AutoCoupon_Model_Couponlog extends Mage_Core_Model_Abstract {
       
        const XML_PATH_CONFIRM_EMAIL_IDENTITY       = 'newsletter/subscription/confirm_email_identity';
        
            protected  function  _construct(){
                $this->_init('autocoupon/couponlog');
            }
            
            public function sendActiveCoupon(){
                
                if(empty($this->getEmail())||empty($this->getEventsCode())||empty($this->getRuleId())){return false;}
                if(!$this->isCouponOfSubscriber()){
                    return false;
                }
                
                 try {
                     $this->sendEmailCoupon();
                     return true;
                 } catch (Exception $e) {
                     return false;
                 }
            }
            public function sendEmailCoupon(){
                 $templateCodeLang ="";
                 $templateCode = $this->getEventsCode();
                 $email = Mage::getModel('core/email_template');
                 $storeStoreId = Mage::app()->getStore()->getStoreId();
                 if($storeStoreId=="2"){
                     $templateCodeLang =$templateCode."_ru"; 
                 }else{
                     $templateCodeLang =$templateCode;
                 }
                 $emailTemplate_model  = $email->loadByCode($templateCodeLang);
                 if(!$emailTemplate_model ->getId()){
                     $templateCodeLang =$templateCode;
                     $emailTemplate_model  = $email->loadByCode($templateCodeLang);
                 }
                 $emailTemplateId =$emailTemplate_model ->getId();
                 $translate = Mage::getSingleton('core/translate');
                /* @var $translate Mage_Core_Model_Translate */
                $translate->setTranslateInline(false);

                $email->sendTransactional(
                   $emailTemplateId,
                    Mage::getStoreConfig(self::XML_PATH_CONFIRM_EMAIL_IDENTITY),
                    $this->getEmail(),
                    $this->getName(),
                    array('autocoupon'=>$this)
                );
                $translate->setTranslateInline(true);

       
            } 
            public function createCoupon(){
                $record=array(
                    "email"=> $this->getEmail(),
                    "events_code"=>$this->getEventsCode(),
                );
                $rule_id=$this->getRuleId();
                if(count($rule_id)>1){
                    foreach ($rule_id as $val ){
                        $code[] = Mage::helper('autocoupon')->getCouponCode($val,$record);
                    }
                }else{
                        $code[] = Mage::helper('autocoupon')->getCouponCode($rule_id,$record);
                }

                return $this->emailStyle($code);
                
            }
             public function isCouponOfSubscriber(){ 
                    $sub_email = $this->getEmail();
                    $events_code = $this->getEventsCode();
                    // $events_model var @SF_PromoteEvents_Helper_Data
                    $events_model = Mage::helper('promoteevents')->getEvents($events_code);
                    $even_info  = $events_model->getEventsData();
                    $from_date = strtotime($even_info->getFromDate());
                    $to_date     = strtotime($even_info->getToDate());
                    $active       =$even_info->getIsActive();
                    $now = Mage::getModel( 'core/date' )->timestamp(time());
                    $couponLog = Mage::getModel("autocoupon/couponlog")->getCollection();
                    $is_subscriber=$couponLog
                    ->addFieldToFilter("events_code",array('eq'=>$events_code))
                    ->addFieldToFilter("email",array('eq'=>$sub_email))
                    ->getData();      

                      if(!$is_subscriber&&$now>=$from_date&&$now<=$to_date&&$active==1) 
                      {
                          return true;
                      }          
              return false;
            }
            /**
             * $code
             **/
            protected  function emailStyle($code){
                $str.="<table style='border:solid #000000; border-width:1px 0px 0px 1px;border-collapse: collapse;text-align: center;width:520px'>";
                $str.="<tr><td style='border:solid #000000; border-width:0px 1px 1px 0px; padding:5px 5px;font-weight: bold;'>".Mage::helper('autocoupon')->__("Discount")."</td><td style='border:solid #000000; border-width:0px 1px 1px 0px; padding:5px 5px;font-weight: bold;'>".Mage::helper('autocoupon')->__("Coupon Code")."</td><td style='border:solid #000000; border-width:0px 1px 1px 0px; padding:5px 5px;font-weight: bold;'>".Mage::helper('autocoupon')->__("Order(s)")."</td></tr>";
                foreach($code as $key => $codes){
                    $str.="<tr><td style='border:solid #000000; border-width:0px 1px 1px 0px; padding:5px 5px;'>".Mage::helper('autocoupon')->__("$".(int)$codes['amount']." OFF")."</td><td style='border:solid #000000; border-width:0px 1px 1px 0px; padding:5px 5px;'>";
                    foreach ($codes['code'] as $cod ){
                        $str.="<p style='font-weight: bold;color:#f2445d'>".$cod."</p>";
                    }
                    $str.="</td><td style='border:solid #000000; border-width:0px 1px 1px 0px; padding:5px 5px;'>".Mage::helper('autocoupon')->__($codes['description'])."</td></tr>";
                }
                $str.="</table>";
                return  $str;
            }
    }